//
//  LeftAttachedCell.m
//  Flava
//
//  Created by Ug-Code on 13-8-14.
//  Copyright (c) 2013年 UgCode. All rights reserved.
//

#import "LeftAttachedCell.h"


@implementation LeftAttachedCell

- (id)initWithStyle:(UGTableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
	_bgView = [[UGImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 217)];
	_buttons = [[NSMutableArray alloc] init];
    if (self) {
        if (style == UGTableViewCellStyle1) {
			_bgView.image = [UIImage imageNamed:@"cell_bg_common2_n.png"];
			_bgView.image = [_bgView.image stretchableImageWithLeftCapWidth:0 topCapHeight:5];
		} else if (style == UGTableViewCellStyle2) {
			_bgView.image = [UIImage imageNamed:@"cell_bg_common3_n.png"];
			_bgView.image = [_bgView.image stretchableImageWithLeftCapWidth:0 topCapHeight:5];
		}
		[self.contentView addSubview:_bgView];
		[self crecteTitleLable];
		[self createButton];
    }
    return self;
}

- (void)setTableCellModel:(LeftTableCellModel *)model
{
	_titleLable.text = model.titleText;
}

- (void)crecteTitleLable
{
	_titleLable = [[UILabel alloc] initWithFrame:CGRectMake(10, 14, 185, 13)];
	_titleLable.font = [UIFont systemFontOfSize:13];
	_titleLable.backgroundColor = UGAlphaColor;
	_titleLable.textColor = [UIColor grayColor];
	[self.contentView addSubview:_titleLable];
}

- (void)createButton
{
	CGFloat button_X = 20;
	CGFloat button_Y = 40;
	CGFloat button_W = 50;
	CGFloat button_H = 50;
	CGFloat OffSet = 6;
	int buttonCount = 9;
	
	for (int i = 0; i < buttonCount; i++) {
		CGRect rect = CGRectMake(button_X+(button_W+OffSet)*(i%4),
								  button_Y+(button_H+OffSet)*(i/4),
								  button_W,
								  button_H);
		UGSwitchButton *button = [[UGSwitchButton alloc] initWithFrame:rect];
		[button setNormalImageWithFile:[NSString stringWithFormat:@"attached_type_icon0%d_n2.png", i]];
		[button setHighlightedImageWithFile:[NSString stringWithFormat:@"attached_type_icon0%d_s.png", i]];
		[button setSelectedImageWithFile:[NSString stringWithFormat:@"attached_type_icon0%d_s.png", i]];
		button.adjustsImageWhenHighlighted = NO; //不要绘制按钮点击时的图片(点击时图片不会变黑)
		button.tag = i+200;
		[button addTarget:self action:@selector(buttonSelected:) forControlEvents:UIControlEventTouchUpInside];
		[_buttons addObject:button];
		[self.contentView addSubview:button];
	}
}

- (void)buttonSelected:(UGSwitchButton *)button
{
	if (button.selected == YES) {
		NSLog(@"Selected:%d", button.tag);
	} else if (button.selected == NO) {
		NSLog(@"DesSelected:%d", button.tag);
	}else {
		LOGLINE NSLog(@"Error!");
	}
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
